#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/7/20 11:45
# @Author  : Sun
# @File    : join_room.py
# @Software: PyCharm
import json

import requests

from common.mqtt_connect import MQTTConnect
# 直接继承base_api
from common.write_log import WriteLog


class ContributionRank(MQTTConnect):
    contribution_rank_result = None
    _URL = 'https://test-api.live.jjmatch.com:21102/live/contributionRank'
    _HEADERS = {
        'platform': 'Android-Host',
        'Authorization': None,
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    _DATA = None

    log = WriteLog().write_log()

    def __init__(self):
        """如果Token不为空就不去重复的调用"""
        # 给headers赋值
        super().__init__()
        if self._HEADERS['Authorization'] is None:
            self._HEADERS['Authorization'] = super().login()

    # 查询贡献榜接口
    def contribution_rank(self, rank_type):
        # WriteLog().write_log().info(self._HEADERS['Authorization'])
        self.log.info('contribution_rank的Headers：' + str(self._HEADERS))
        self._DATA = f'type={rank_type}&roomId=300001&pageNum=1&pageSize=25'
        self.log.info('contribution_rank的Data：' + self._DATA)
        r = requests.request(method='POST', url=self._URL, headers=self._HEADERS, data=self._DATA)
        self.contribution_rank_result = json.dumps(r.json(), ensure_ascii=False, indent=4)
        WriteLog().write_log().info('contribution_rank的请求结果：' + str(self.contribution_rank_result))
        return self.contribution_rank_result


if __name__ == '__main__':
    ContributionRank().contribution_rank(2)
